package org.dylan.bdf.system.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.dylan.bdf.system.dao.LogDao;
import org.dylan.bdf.system.entity.Log;
import org.dylan.bdf.system.entity.User;
import org.dylan.bdf.system.model.LogLevel;
import org.dylan.bdf.system.model.LogType;
import org.dylan.security.entity.IUser;
import org.dylan.security.util.SecurityUtils;
import org.dylan.web.util.BrowserUtils;
import org.dylan.web.util.RequestHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 系统日志组件
 * 
 * @author dylan
 * 
 */
@Service
public class LogService {

	@Autowired
	private LogDao logDao;

	/**
	 * 增加一条日志信息
	 * 
	 * @param logLevel
	 * @param logType
	 * @param content
	 */
	public void addLog(LogLevel logLevel, LogType logType, String content) {
		HttpServletRequest request = RequestHolder.getRequest();
		IUser user = SecurityUtils.getUser();
		Log log = new Log();
		log.setLogLevel(logLevel);
		log.setLogType(logType==null?null:logType.key());
		log.setLogUser(user==null ? null : user.getUsername());
		log.setContent(content);
		log.setOperateTime(new Date());
		log.setIpAddress(request.getRemoteHost());
		log.setBrowser(BrowserUtils.getBrowserType(request).name());
		log.setWebkit(request.getHeader("USER-AGENT"));
		logDao.save(log);
	}

	public LogDao getLogDao() {
		return logDao;
	}

	public void setLogDao(LogDao logDao) {
		this.logDao = logDao;
	}

}
